package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.qurey.RegionQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/region")
public class RegionController {

    @Autowired
    private IRegionService regionService;

    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private DestinationRepository destinationRepository;

    @RequestMapping("/list")
    private String list(Model model,@ModelAttribute("qo") RegionQuery qo){
        Page<Region> page= regionService.query(qo);
        model.addAttribute("page", page);
        return "region/list";
    }

    @GetMapping("listDests")
    @ResponseBody
    private Object listDests(){
        return destinationService.list();
    }

    @PostMapping("saveOrUpdate")
    @ResponseBody
    private Object saveOrUpdate(Region region){
           regionService.saveOrUpdate(region);
        return JsonResult.success();
    }

    @GetMapping("getDestByRegionId")
    @ResponseBody
    private Object getDestByRegionId(String rid){

        List<Destination> list = destinationService.queryDestByRegionId(rid);
        return list;
    }

    @GetMapping("changeHotValue")
    @ResponseBody
    private Object changeHotValue(int hot,String id){
        regionService.changeHotValue(hot,id);
        return JsonResult.success();
    }

    @GetMapping("delete")
    @ResponseBody
    private Object delete(String id){
        regionService.delete(id);
        return JsonResult.success();
    }

    @GetMapping("/getDestByDeep")
    @ResponseBody
    private Object getDestByDeep(int deep){
        return destinationRepository.findByDeep(deep);

    }
}
